package pw.ucoin.www.customview2

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View

/**
 * Created by lgs on 2018/7/19.
 */
class GradientView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val shader1: Shader = LinearGradient(100f, 100f, 500f, 500f,
            Color.parseColor("#E91E63"), Color.parseColor("#2196F3"),
            Shader.TileMode.CLAMP)

    private val shader2: Shader = RadialGradient(300f, 300f, 200f, Color.parseColor("#E91E63"),
            Color.parseColor("#2196F3"), Shader.TileMode.CLAMP)

    private val shader3: Shader = SweepGradient(300f, 300f, Color.parseColor("#E91E63"),
            Color.parseColor("#2196F3"))

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)

    override fun onDraw(canvas: Canvas?) {

//        canvas?.save()
//        paint.shader = shader1
//        canvas?.drawCircle(300f, 300f, 200f, paint)
//        canvas?.restore()

//        canvas?.save()
//        paint.reset()
//        paint.shader = shader2
//        canvas?.drawCircle(300f, 300f, 200f, paint)
//        canvas?.restore()

        canvas?.save()
        paint.shader = shader3
        canvas?.drawCircle(300f, 300f, 200f, paint)
        canvas?.restore()

    }
}